'''可以通过自定义类重载运算符实现列表的数值运算功能。以下是一个完整的实现方案：'''


class NumList:
    """支持数值运算的自定义列表类"""

    def __init__(self, values):
        self.values = values

    def __add__(self, other):
        """支持列表与数字相加"""
        if isinstance(other, (int, float)):
            return NumList([x + other for x in self.values])
        if isinstance(other, NumList):
            return NumList([x + y for x, y in zip(self.values, other.values)])
        return NotImplemented

    def __sub__(self, other):
        """支持列表与数字相减"""
        if isinstance(other, (int, float)):
            return NumList([x - other for x in self.values])
        if isinstance(other, NumList):
            return NumList([x - y for x, y in zip(self.values, other.values)])
        return NotImplemented

    def __mul__(self, other):
        """支持列表与数字相乘"""
        if isinstance(other, (int, float)):
            return NumList([x * other for x in self.values])
        if isinstance(other, NumList):
            return NumList([x * y for x, y in zip(self.values, other.values)])
        return NotImplemented

    def __truediv__(self, other):
        """支持列表与数字相除"""
        if isinstance(other, (int, float)):
            if other == 0:
                raise ZeroDivisionError("Division by zero")
            return NumList([x / other for x in self.values])
        if isinstance(other, NumList):
            if any(y == 0 for y in other.values):
                raise ZeroDivisionError("Division by zero in NumList")
            return NumList([x / y for x, y in zip(self.values, other.values)])
        return NotImplemented

    def __radd__(self, other):
        """支持数字 + 列表的反向操作"""
        return self + other

    def __rsub__(self, other):
        """支持数字 - 列表的反向操作"""
        if isinstance(other, (int, float)):
            return NumList([other - x for x in self.values])
        return NotImplemented

    def __rmul__(self, other):
        """支持数字 * 列表的反向操作"""
        return self * other

    def __rtruediv__(self, other):
        """支持数字 / 列表的反向操作"""
        if isinstance(other, (int, float)):
            return NumList([other / x for x in self.values])
        return NotImplemented

    def __str__(self):
        return f"NumList({self.values})"

    def __repr__(self):
        return str(self)


# 测试用例
if __name__ == "__main__":
    # 创建数值列表
    lst = NumList([1, 2, 3, 4])

    # 列表与数字运算
    print(lst + 2)  # 输出: NumList([3, 4, 5, 6])
    print(5 + lst)  # 输出: NumList([6, 7, 8, 9])
    print(lst - 1)  # 输出: NumList([0, 1, 2, 3])
    print(10 - lst)  # 输出: NumList([9, 8, 7, 6])
    print(lst * 3)  # 输出: NumList([3, 6, 9, 12])
    print(2 * lst)  # 输出: NumList([2, 4, 6, 8])
    print(lst / 2)  # 输出: NumList([0.5, 1.0, 1.5, 2.0])
    print(10 / lst)  # 输出: NumList([10.0, 5.0, 3.33..., 2.5])

    # 列表与列表运算
    lst2 = NumList([10, 10, 10, 10])
    print(lst + lst2)  # 输出: NumList([11, 12, 13, 14])
    print(lst * lst2)  # 输出: NumList([10, 20, 30, 40])

    # 链式运算
    result = (lst + 5) * 2 - lst2
    print(result)  # 输出: NumList([12, 14, 16, 18])

    # 错误处理
    try:
        print(lst / 0)
    except ZeroDivisionError as e:
        print(f"Error: {e}")  # 输出: Error: Division by zero

    try:
        print(5 / NumList([1, 0, 3]))
    except ZeroDivisionError as e:
        print(f"Error: {e}")  # 输出: Error: Division by zero in NumList